/**
 * 
 */
package com.ofek.eventos.net.lks.test;

import java.io.IOException;

import com.ofek.eventos.net.lks.LastKnownSyncer;
import com.ofek.eventos.net.lks.ServerDatabaseHandle;
import com.ofek.eventos.net.lks.Sync;
import com.ofek.eventos.net.ocsf.server.API;
import com.ofek.eventos.net.ocsf.server.Service;

/**
 * @author Ofek
 *
 */
public class ServerDB implements API {
	final private String uid = "Server";

	private final LastKnownSyncer<RequestID, Request> requestsSyncer;

	private final LastKnownSyncer<EventID, EventData> eventsSyncer;

	private final Service service;

	/**
	 * @param port 
	 * @throws IOException 
	 * 
	 */
	public ServerDB(final int port) throws IOException {
		service = new Service(port, this);
		requestsSyncer = new LastKnownSyncer<RequestID, Request>(uid,new ServerDatabaseHandle<RequestID, Request>(),null );
		eventsSyncer = new LastKnownSyncer<EventID, EventData>(uid,new ServerDatabaseHandle<EventID, EventData>(), null);
	}

	public Sync<EventID, EventData> syncEvents(final Sync<EventID, EventData> sync) throws IOException {
		return eventsSyncer.syncIteration(sync);
	}
	public Sync<RequestID, Request> syncRequests(final Sync<RequestID,Request> sync) throws IOException {
		return requestsSyncer.syncIteration(sync);
	}
	/**
	 * 
	 */
	public void close() {
		requestsSyncer.close();
		eventsSyncer.close();
		service.close();
	}
}
